//页面gui
var objScreen = {
    //默认展现基本信息
    Source:"就诊信息",
    episodeId:"",
    mrListId:"",
    formItemId:""
}

$(function () {
    objScreen.episodeId = $("#episodeId").val();
    objScreen.mrListId = $("#mrListId").val();
    objScreen.formItemId = $("#formItemId").val();
    initEpisodeInfo();
    //默认展现基本信息
    InitviewScreen();
});

//导航信息
function initEpisodeInfo(){
    var name = $("#episodeName").val();
    var age = $("#episodeAge").val();
    var gender = $("#episodeGender").val();
    var mrNum = $("#episodeMrNum").val();
    var admLocDesc = $("#episodeAdmLocDesc").val();
    var patAdmDate = $("#episodeAdmDate").val();
    var episodeStr= "&nbsp;&nbsp;" + name + "&nbsp;&nbsp;" + age + "岁|" + gender + "&nbsp;&nbsp;病案号:" + mrNum + " &nbsp;&nbsp;" + admLocDesc + "&nbsp;&nbsp;"  + "入院时间：" + patAdmDate
    $("#PatInfo").html(episodeStr);
}

function InitviewScreen(){
    $.parser.parse(); // 解析整个页面
    switch (objScreen.Source) {
        case "就诊信息": case "编目信息":
            var title="就诊信息"
            break;
        case "费用信息":
            var title="费用信息"
            break;
        case "非抗菌药物": case "抗菌药物": case "处置操作":
            var title="医嘱信息"
            break;
        case "检验生化":
            var title="检验信息"
            break;
        case "影响检查":
            var title="检查信息"
            break;
        case "手术信息":
            var title="手术信息"
            break;
        case "生命体征":
            var title="生命体征"
            break;
        case "电子病历":
            var title="电子病历"
            break;
        default:
            var title="就诊信息"
            break;
    }
    $('#SumT').tabs('select',title);

    //InitPatInfo();

    /*InitviewScreenEvent(objScreen);
    objScreen.LoadEvent(arguments);*/
}

//基本信息
function InitPatInfo(){
    var ax1 = new $ax("/sds/sdsqcformshow/getDataByAdm", function (data) {
        if (data) {
            var jsonObj=JSON.parse(data)
            for(var key in jsonObj){
                var value=jsonObj[key].toString();
                if (!value) continue;
                if (value.substr(0,1)=="*"){
                    $('#'+key).val(value.substr(1,value.length));
                    $('#'+key).css({"background-color":"#6293BB","color":"#fff"});
                }else{
                    $('#'+key).val(value)
                }
            }
        }
    });
    ax1.set("episodeId", objScreen.episodeId);
    ax1.start();
}

//费用信息
function InitFeeInfo(){
    var ax = new $ax("/sds/sdsqcformshow/getDataByFee", function (data) {
        if (data) {
            var jsonObj=JSON.parse(data)
            for(var key in jsonObj){
                var value=jsonObj[key].toString();
                if (!value) continue;
                if (value.substr(0,1)=="*"){
                    $('#'+key).val(value.substr(1,value.length));
                    $('#'+key).css({"background-color":"#6293BB","color":"#fff"});
                }else{
                    $('#'+key).val(value)
                }
            }
        }
    });
    ax.set("episodeId", objScreen.episodeId);
    ax.start();
}

//医嘱信息
function InitOrderInfo(){
    objScreen.gridOrderInfo = $HUI.datagrid("#gridOrderInfo",{
        fit:true,
        title: "<b>医嘱单</b>",
        headerCls:'panel-header-gray',
        //pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        //displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: true,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        nowrap:false,
        //striped:true,
        //rownumbers:true,
        //checkOnSelect: false,
        //selectOnCheck: true,
        //onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        //pageSize: 10,
        //pageList : [10,50],
        url:"/sds/sdsqcformshow/getDataByOrder",
        method: 'post',
        queryParams:{
            episodeId: objScreen.episodeId,
            mrListId: objScreen.mrListId,
            formItemId: objScreen.formItemId
        },
        idField:"id",
        columns:[[
            {field:'code',title:'医嘱代码',width:'80',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                }},
            {field:'priority',title:'医嘱类型',width:'80',align:'center'},
            {field:'description',title:'医嘱名称',width:'250',sortable:true},
            {field:'status',title:'医嘱状态',width:'80',sortable:true},
            {field:'orderDate',title:'开医嘱时间',width:'120',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'firstExecDate',title:'首次执行时间',width:'120',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'lastExecDate',title:'末次执行时间',width:'120',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'endDate',title:'停医嘱时间',width:'120',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
        ]]

    });
}

function InitLisInfo(){
    objScreen.gridLisReport = $HUI.datagrid("#gridLisReport",{
        fit:true,
        title: "<b>检验报告记录</b>",
        headerCls:'panel-header-gray',
        //pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        //displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: true,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        nowrap:false,
        //striped:true,
        //rownumbers:true,
        //checkOnSelect: false,
        //selectOnCheck: true,
        //onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        //pageSize: 10,
        //pageList : [10,50],
        url:"/sds/sdsqcformshow/getDataByLisReport",
        method: 'post',
        queryParams:{
            episodeId: objScreen.episodeId,
            mrListId: objScreen.mrListId,
            formItemId: objScreen.formItemId
        },
        idField:"id",
        columns:[[
            {field:'labOrdDesc',title:'检验医嘱',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'reportStatus',title:'报告状态',width:'80',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'specimen',title:'标本',width:'60',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'recDate',title:'送检日期',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'repDate',title:'报告时间',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }}

        ]],
        rowStyler: function(index,row){
            /*if (row.OEItemID.indexOf('*')==0){
                return 'background-color:#6293BB;color:#fff;'; // return inline style
            }*/
        },
        onSelect:function(rindex,rowData){
            InitLisResult(rowData.reportId);
        },
        onDblClickRow:function(rowIndex,rowData){

        },
        onLoadSuccess:function(data){
            /*for (var i=0;i<data.rows.length;i++) {
                var rec=data.rows[i]
                if(rec.OEItemID.indexOf('#')==0){
                    $(this).datagrid('selectRow' ,i);
                }
            }*/
        }

    });
}

function InitLisResult(reportId){
    objScreen.gridLisResult = $HUI.datagrid("#gridLisResult",{
        fit:true,
        title: "<b>结果明细</b>",
        headerCls:'panel-header-gray',
        //pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        //displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: true,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        nowrap:false,
        //striped:true,
        //rownumbers:true,
        //checkOnSelect: false,
        //selectOnCheck: true,
        //onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        //pageSize: 10,
        //pageList : [10,50],
        url:"/sds/sdsqcformshow/getDataByLisResult",
        method: 'post',
        queryParams:{
            episodeId: objScreen.episodeId,
            mrListId: objScreen.mrListId,
            formItemId: objScreen.formItemId,
            reportId: reportId
        },
        idField:"id",
        columns:[[
            {field:'itemCode',title:'项目代码',width:'70',sortable:true},
            {field:'itemDesc',title:'项目名称',width:'100',sortable:true},
            {field:'itemResult',title:'结果值',width:'150',align:'center',
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'resultRange',title:'参考值',width:'120',align:'center'},
            {field:'labAbFlag',title:'是否异常',width:'70',align:'center',
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }}

        ]],
        onSelect:function(rindex,rowData){

        },
        onDblClickRow:function(rowIndex,rowData){

        },
        onLoadSuccess:function(data){

        }

    });
}

function InitRisReport(){
    objScreen.gridRisReport = $HUI.datagrid("#gridRisReport",{
        fit:true,
        title: "<b>检查报告记录</b>",
        headerCls:'panel-header-gray',
        //pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        //displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: true,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        nowrap:false,
        //striped:true,
        //rownumbers:true,
        //checkOnSelect: false,
        //selectOnCheck: true,
        //onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        //pageSize: 10,
        //pageList : [10,50],
        url:"/sds/sdsqcformshow/getDataByRisReport",
        method: 'post',
        queryParams:{
            episodeId: objScreen.episodeId,
            mrListId: objScreen.mrListId,
            formItemId: objScreen.formItemId
        },
        idField:"id",
        columns:[[
            {field:'ordMastDesc',title:'检查医嘱',width:'100',sortable:true},
            {field:'examNum',title:'检查号',width:'80',sortable:true},
            {field:'reportStatus',title:'状态',width:'100',sortable:true},
            {field:'repDate',title:'报告时间',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }}

        ]],
        rowStyler: function(index,row){
            /*if (row.OEItemID.indexOf('*')==0){
                return 'background-color:#6293BB;color:#fff;'; // return inline style
            }*/
        },
        onSelect:function(rindex,rowData){
            /*Common_SetValue('RBStudyNo',((rowData.RBStudyNo.indexOf('*')==0)?rowData.RBStudyNo.substr(1,rowData.RBStudyNo.length):rowData.RBStudyNo));
            Common_SetValue('RepDateT',((rowData.RepDateT.indexOf('*')==0)?rowData.RepDateT.substr(1,rowData.RepDateT.length):rowData.RepDateT));
            $('#RepResult').html(rowData.RepResult);
            $('#RepDianog').html(rowData.RepDianog);*/
        },
        onDblClickRow:function(rowIndex,rowData){

        },
        onLoadSuccess:function(data){
            /*for (var i=0;i<data.rows.length;i++) {
                var rec=data.rows[i]
                if(rec.OEItemID.indexOf('#')==0){
                    $(this).datagrid('selectRow' ,i);
                }else {
                    for (var Key in rec){
                        if (rec[Key].indexOf('*')==0){
                            $(this).datagrid('selectRow' ,i);
                        }
                    }
                }
            }*/
        }

    });
}

function InitOperInfo(){
    /*var ax = new $ax("/sds/sdsqcformshow/getDataByFee", function (data) {
        if (data) {
            var jsonObj=JSON.parse(data)
            for(var key in jsonObj){
                var value=jsonObj[key].toString();
                if (!value) continue;
                if (value.substr(0,1)=="*"){
                    $('#'+key).val(value.substr(1,value.length));
                    $('#'+key).css({"background-color":"#6293BB","color":"#fff"});
                }else{
                    $('#'+key).val(value)
                }
            }
        }
    });
    ax.set("episodeId", objScreen.episodeId);
    ax.start();*/
}

function InitNurInfo(){
    objScreen.gridNurInfo = $HUI.datagrid("#gridNurInfo",{
        fit:true,
        title: "<b>体温单记录</b>",
        headerCls:'panel-header-gray',
        //pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        //displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: true,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        nowrap:false,
        //striped:true,
        //rownumbers:true,
        //checkOnSelect: false,
        //selectOnCheck: true,
        //onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        //pageSize: 10,
        //pageList : [10,50],
        url:"/sds/sdsqcformshow/getDataByNur",
        method: 'post',
        queryParams:{
            episodeId: objScreen.episodeId,
            mrListId: objScreen.mrListId,
            formItemId: objScreen.formItemId
        },
        idField:"id",
        columns:[[
            {field:'recDate',title:'记录日期',width:'120',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }

                }
            },
            {field:'SBP',title:'收缩压',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'DBP',title:'舒张压',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'pulse',title:'脉搏',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'breath',title:'呼吸频率',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'heartRate',title:'心率',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'height',title:'身高(CM)',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'weight',title:'体重(KG)',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'satO2',title:'血氧饱和度',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }},
            {field:'braden',title:'压疮评估',width:'100',sortable:true,
                formatter:function ( value,row,index ){
                    if (value.indexOf('*')==0){
                        return value.substr(1,value.length)
                    } else {
                        return value;
                    }
                },
                styler: function(value,row,index){
                    if (value.indexOf('*')==0){
                        return 'background-color:#6293BB;color:#fff;';
                    }
                }}

        ]],
        onSelect:function(rindex,rowData){

        },
        onDblClickRow:function(rowIndex,rowData){

        },
        onLoadSuccess:function(data){

        }

    });
}

function InitEMRInfo(){
    $("#EMRDoclist").keywords({
        singleSelect:true,
        items:[{
            text:"病历类型",
            type:"section",
            items:[
                {text:'入院记录',id:"InHosRec"},
                {text:'首次病程',id:"FirstRec"},
                {text:'日常病程',id:'DailyRec'},
                {text:'主治医师查房',id:'DocRec'},
                {text:'手术记录',id:'OperRec'},
                {text:'查体记录',id:'BodyRec'},
                {text:'术后小结',id:'AfOperRec'},
                {text:'出院记录',id:'OutHosRec'}
            ]
        }],
        onUnselect:function(v){console.log("你选择->");console.dir(v);},
        onSelect:function(v){console.log("你选择->");console.dir(v);},
        onClick:function(item){
            //加载病历内容

            //质控项目类型与结果值

            //加载质控项目选项内容


        }
    })
    $("#EMRDoclist").keywords('select','DailyRec');
}

/**
 * 页签选择事件
 */
objScreen.changeTabs = function (title, index) {
    objScreen.episodeId = $("#episodeId").val();
    objScreen.mrListId = $("#mrListId").val();
    objScreen.formItemId = $("#formItemId").val();
    switch (index) {
        case 0:
            //就诊信息
            InitPatInfo();
            break;
        case 1:
            //费用信息
            InitFeeInfo();
            break;
        case 2:
            //医嘱信息
            InitOrderInfo();
            break;
        case 3:
            //检验信息
            InitLisInfo();
            InitLisResult("");
            break;
        case 4:
            //检查报告信息
            InitRisReport();
            break;
        case 5:
            //手麻信息
            InitOperInfo();
            break;
        case 6:
            //生命体征信息
            InitNurInfo();
            break;
        case 7:
            //病历信息
            InitEMRInfo();
            break;
        default:
            InitPatInfo();
            break;
    };
}

//页面Event
function InitviewScreenEvent(obj){
    obj.LoadEvent = function(args){
        document.oncontextmenu = function(){
            if(window.getSelection){  //FF
                var FocusText= window.getSelection().toString();
            }else{ //IE
                var FocusText= document.selection.createRange().text;
            }
            if (FocusText=="") return;
            var event = event || window.event;
            //固定位置加载选中内容以及值域字典
            var Originalhtml=$('#selectText').val()
            if (Originalhtml=="") {
                var html=FocusText
            }else{
                var html=Originalhtml+"\r\n"+FocusText
            }
            $('#selectText').val(html)
            /*跟随选中内容，打开webui
            $("#DicList").webuiPopover('destroy');
            $('#DicList').css({"top":event.clientY,"left":event.clientX})
            $("#DicList").webuiPopover({
                trigger:'manual',
                placement:'auto',
                title:'',
                type:'html',
                padding:'10px',
                cache:false,
                multi:true,
                content:FocusText,//backdrop:true,
                delay:{show: null,hide:500}
            });
            $("#DicList").webuiPopover('show');
            */
            return false;
        }
        //更新主表单对应单元格的值
        obj.updateForm=function(code,text){
            var tIndex=window.opener.$('#gridQCFormShow').datagrid('getRowIndex',obj.ItemCode);
            if (tIndex) {
                window.opener.$("#gridQCFormShow").datagrid("updateRow",{
                    index:tIndex, //行索引
                    row:{
                        ExecResult:code,
                        ExecResultText:text
                    }
                })
            }
        }
        /*$HUI.radio("[name='"+ItemID+"']",{
            onChecked:function(e,value){
                obj.updateForm($(this)[0].id,$(this)[0].title)
            }
        })
        $('input:checkbox').click(function(){
            var Codes="",DicStrs=""
            $("input:checkbox[name='"+ItemID+"']:checked").each(function(i){
                if(0==i){
                    Codes = $(this)[0].id;
                    DicStrs=$(this)[0].title
                }else{
                    Codes += (","+$(this)[0].id);
                    DicStrs +=(","+ $(this)[0].title);
                }
            });
            obj.updateForm(Codes,DicStrs)
        })*/
    }
}
